package com.sunny.excel.excel.common;

import com.google.common.collect.Lists;
import com.sunny.excel.excel.build.ExcelBuilder;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


import java.util.List;


public abstract class AbstractExcelBuilder<T> implements ExcelBuilder<T> {

    @Override
    public void buildExcel(List<T> list, Workbook workbook, int sheetIndex) {
        Sheet sheet = createSheet(workbook, list, sheetIndex);
        Row header = sheet.createRow(0);
        buildHeader(list, header, sheetIndex, Lists.newArrayList());
        buildContent(list, sheet, sheetIndex);
    }

    @Override
    public void buildExcel(List<T> list, Workbook workbook, int sheetIndex, List<String> dynamicHeaderList) {
        Sheet sheet = createSheet(workbook, list, sheetIndex);
        Row header = sheet.createRow(0);
        buildHeaderWithDynamicHeader(list, header, sheetIndex, dynamicHeaderList);
        buildContent(list, sheet, sheetIndex);
    }

    protected abstract Sheet createSheet(Workbook workbook, List<T> list, int sheetIndex);

    protected abstract void buildContent(List<T> list, Sheet sheet, int sheetIndex);


    protected abstract void buildHeader(List<T> list, Row header, int sheetIndex, List<String> dynamicHeaderList);

    protected void buildHeaderWithDynamicHeader(List<T> list, Row header, int sheetIndex,
        List<String> dynamicHeaderList) {
        buildHeader(list, header, sheetIndex,dynamicHeaderList);
    }

    protected String maybeNull(String str) {
        if (str == null) {
            return "";
        }
        return str;
    }

}
